#include <stdlib.h>
#include <string.h>
#include "token.h"

// 创建新的Token对象
// 参数：
// - type: Token类型
// - lexeme: Token对应的字符串内容
// - line: Token所在行号
// - column: Token所在列号
// 返回值：创建的Token指针，失败返回NULL
Token* create_token(TokenType type, const char* lexeme, int line, int column) {
    Token* token = (Token*)malloc(sizeof(Token));
    token->type = type;
    token->lexeme = strdup(lexeme);
    token->line = line;
    token->column = column;
    return token;
}

// 释放Token对象及其资源
// 参数：
// - token: 要释放的Token指针
void free_token(Token* token) {
    if (token) {
        free(token->lexeme);
        free(token);
    }
}

// 将Token类型转换为对应的字符串表示
// 参数：
// - type: Token类型
// 返回值：Token类型的字符串表示
const char* token_type_to_string(TokenType type) {
    switch (type) {
        case SELECT: return "SELECT";
        case FROM: return "FROM";
        case WHERE: return "WHERE";
        case CREATE: return "CREATE";
        case TABLE: return "TABLE";
        case INSERT: return "INSERT";
        case INTO: return "INTO";
        case VALUES: return "VALUES";
        case DELETE: return "DELETE";
        case IF: return "IF";
        case EXISTS: return "EXISTS";
        case NOT: return "NOT";
        case DROP: return "DROP";
        case ALTER: return "ALTER";
        case SET: return "SET";
        case UPDATE: return "UPDATE";
        case ADD: return "ADD";
        case MODIFY: return "MODIFY";
        case COLUMN: return "COLUMN";
        case INT: return "INT";
        case VARCHAR: return "VARCHAR";
        case FLOAT: return "FLOAT";
        case DOUBLE: return "DOUBLE";
        case IDENTIFIER: return "IDENTIFIER";
        case NUMBER: return "NUMBER";
        case STRING: return "STRING";
        case EQUAL: return "EQUAL";
        case NOT_EQUAL: return "NOT_EQUAL";
        case LESS: return "LESS";
        case LESS_EQUAL: return "LESS_EQUAL";
        case GREATER: return "GREATER";
        case GREATER_EQUAL: return "GREATER_EQUAL";
        case PLUS: return "PLUS";
        case MINUS: return "MINUS";
        case MULTIPLY: return "MULTIPLY";
        case DIVIDE: return "DIVIDE";
        case LEFT_PAREN: return "LEFT_PAREN";
        case RIGHT_PAREN: return "RIGHT_PAREN";
        case COMMA: return "COMMA";
        case SEMICOLON: return "SEMICOLON";
        case DOT: return "DOT";
        case EOF_TOKEN: return "EOF_TOKEN";
        case ERROR: return "ERROR";
        default: return "UNKNOWN";
    }
}